package org.wattdepot.cli;

import java.util.List;
import org.wattdepot.client.BadXmlException;
import org.wattdepot.client.MiscClientException;
import org.wattdepot.client.NotAuthorizedException;
import org.wattdepot.client.ResourceNotFoundException;
import org.wattdepot.util.tstamp.Tstamp;
import javax.xml.datatype.XMLGregorianCalendar;

/**
 * Outputs the total of energy or carbon generated in one day using intervals
 * inputed for one plant.
 * @author Wing Cheung Ma, Edwin Lim
 *
 */
public class ListTotal extends CommandLineInterface implements WattDepotCliCommand {

  /**
   * Takes the input from the command line and generates the data using wattdepot class.
   * @param args a list of input info
   * @return a string with the sensor data
   */
  @Override
  public String doCommand(List<String> args) {
    
    String process = args.get(0);
    String source = args.get(1);
    String day = args.get(2);
    String interval = args.get(3);
    
    XMLGregorianCalendar theTime = null;
    XMLGregorianCalendar endTime = null;
    try {
      theTime = Tstamp.makeTimestamp(day);
    }
    catch (Exception e) {
      System.out.println("should never happen.");
    }  
    Integer intervalInt = 1;
    
    try {
      intervalInt = Integer.parseInt(interval);
    }
    catch (NumberFormatException e) {
      return "Problem with the interval inputted";
    }
    
    intervalInt = 1440 / intervalInt;
    String totalGenerated = "";
    String carbon = "carbon";
    String energy = "energy";
    endTime = Tstamp.incrementDays(theTime,1);

    if (process.equalsIgnoreCase(carbon)) {
      try {
        totalGenerated = Double.toString(client.getCarbonEmitted(source, theTime,
            endTime, intervalInt));
        
      }
      catch (NotAuthorizedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      catch (ResourceNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      catch (BadXmlException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      catch (MiscClientException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
    
    else if (process.equalsIgnoreCase(energy)) {
      try {
        totalGenerated = Double.toString(client.getEnergyGenerated(source, theTime,
            endTime, intervalInt));
      }
      catch (NotAuthorizedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      catch (ResourceNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      catch (BadXmlException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      catch (MiscClientException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }   
    
    return totalGenerated;
  }
  
}
